# -*- coding: utf-8 -*-
"""
豆瓣电影数据爬取并保存到数据库的脚本
"""

from peewee import *
from bs4 import BeautifulSoup

# 数据库配置
db = MySQLDatabase(
    "douban_db", host="localhost", port=3306, user="root", passwd="123456"
)

# 数据库模型
class Movie(Model):
    """电影数据模型"""
    title = CharField(max_length=200)
    rating_num = FloatField()
    comment_num = IntegerField()
    directors = CharField(max_length=200)
    actors = CharField(max_length=200)
    year = CharField(max_length=10)
    country = CharField(max_length=100)
    category = CharField(max_length=100)
    pic = CharField(max_length=200)

    class Meta:
        database = db
        table_name = "douban_movie"

# 数据保存函数
def save_data(movie_data):
    """
    将电影数据保存到数据库
    :param movie_data: 包含电影信息的字典
    """
    try:
        movie = Movie(**movie_data)
        movie.save()
    except Exception as e:
        print(f"保存数据时出错: {e}")

# 主程序
def main():
    """主程序逻辑"""
    try:
        db.connect()
        print("数据库连接成功")
    except Exception as e:
        print(f"数据库连接失败: {e}")
        return

    # 创建表
    db.create_tables([Movie])

    # 读取 HTML 文件
    html_file_path = r"C:\Users\39737\Desktop\photograpy\douban.html"
    with open(html_file_path, "r", encoding="utf-8") as f:
        html = f.read()

    # 解析 HTML
    page_sections = html.split('<ol class="grid_view">')[1:]
    for section in page_sections:
        section = '<ol class="grid_view">' + section
        soup = BeautifulSoup(section, "lxml")
        movie_list = soup.find("ol", class_="grid_view").find_all("li")
        for movie in movie_list:
            # 提取电影信息
            title = movie.find("div", class_="hd").find("span", class_="title").get_text()
            rating_num = float(
                movie.find("div", class_="bd")
                .find("div")
                .find("span", class_="rating_num")
                .get_text()
            )
            comment_num = int(
                movie.find("div", class_="bd")
                .find("div")
                .find_all("span")[-1]
                .get_text()
                .strip("人评价")
            )
            directors_info = (
                movie.find("div", class_="bd").find("p").get_text().strip().split("\n")[0].strip()
            )
            directors = directors_info.split("导演: ")[1].split("主演: ")[0].strip()
            actors = (
                directors_info.split("主演: ")[1].strip()
                if "主演: " in directors_info
                else ""
            )
            info = (
                movie.find("div", class_="bd")
                .find("p")
                .get_text()
                .strip()
                .split("\n")[1]
                .strip()
            )
            year = info.split("/")[0].strip()
            country = info.split("/")[1].strip()
            category = info.split("/")[2].strip()
            pic = (
                movie.find("div", class_="item")
                .find("div", class_="pic")
                .find("a")
                .find("img")
                .get("src")
            )

            # 保存到数据库
            movie_data = {
                "title": title,
                "rating_num": rating_num,
                "comment_num": comment_num,
                "directors": directors,
                "actors": actors,
                "year": year,
                "country": country,
                "category": category,
                "pic": pic,
            }
            save_data(movie_data)

    db.close()
    print("数据抽取并保存到数据库完成。")

if __name__ == "__main__":
    main()